import {Component, ViewChild} from '@angular/core';
import {Content, IonicPage, NavController, NavParams, Select} from 'ionic-angular';
import {LiveShowDetailProvider} from "../../../../providers/my-team/live-show-detail/live-show-detail.provider";
import {LiveShowDetailModel} from "../../../../model/pages/my-team/live-show/live-show-detail.model";
import {LoadingService} from "../../../../services/common/loading.service";
import {AdpActionSheetController} from "../../../../components/common/adp-action-sheet/adp-action-sheet-controller";

@IonicPage()
@Component({
    selector: 'page-live-show-detail',
    templateUrl: 'live-show-detail.html',
    providers: [
        LiveShowDetailProvider,
    ]
})
export class LiveShowDetailPage {

    isScrolling = false;
    liveShowName;
    liveShowTime;
    isLoaded: boolean = false;
    parms = {
        'groupID': this.navParams.data.groupId,
        'webcastID': this.navParams.data.id,
        'isWatched': '',
        'isPlayBack': ''
    };
    liveShowDetailData: LiveShowDetailModel = new LiveShowDetailModel();

    @ViewChild(Content) content: Content;

    @ViewChild("select1") select1: Select;
    @ViewChild("select2") select2: Select;

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                public liveShowDetailPro: LiveShowDetailProvider,
                public load: LoadingService,
                public adpActionSheet: AdpActionSheetController) {
        if (this.navParams.data.groupId) {
            this.groupId = this.navParams.data.groupId;
        }

    }

    ionViewDidLoad() {
        this.getLiveShowDetail(this.parms);
        this.liveShowName = this.navParams.data.name;
        this.liveShowTime = this.navParams.data.time;

    }

    ionViewWillLeave() {
        this.select1.close();
        this.select2.close();
    }

    private currentGroupName = '全部';

    /**
     * 打开小组
     */

    openDetailSelect() {

        let jobArray = [];

        //
        // for(let m in this.liveShowDetailData.groupList){
        //     let obj = {};
        //     obj['text']=this.liveShowDetailData.groupList[m].name;
        //     obj['cssClass'] = this.currentGroupName == this.liveShowDetailData.groupList[m].name?'action-sheet-selected':'';
        //     obj['handler'] = ()=>{
        //         console.log(m)
        //         this.currentGroupName = this.liveShowDetailData.groupList[m].name;
        //         console.log(this.liveShowDetailData.groupList[m].name,'123');
        //         this.chooseDetailGroup(this.liveShowDetailData.groupList[m].id);
        //     };
        //     jobArray.push(obj);
        //     console.log(jobArray)
        // }

        this.liveShowDetailData.groupList.forEach((item) => {
            let obj = {};
            obj['text'] = item.name;
            obj['cssClass'] = this.currentGroupName == item.name ? 'action-sheet-selected' : '';
            obj['handler'] = (e) => {
                console.log(e);
                this.currentGroupName = item.name;
                this.chooseDetailGroup(item.id);
            };
            jobArray.push(obj);
        });
        this.adpActionSheet.create({
            title: '选择组',
            buttons: jobArray,
            cssClass: 'mySelect'
        }).present();
    }

    private groupId = '';

    /**
     * 选择小组
     */
    chooseDetailGroup(ev) {
        this.parms.groupID = ev;
        this.groupId = ev;
        this.getLiveShowDetail(this.parms)
    }

    private isWatched = '';

    /**
     * 是否观看
     */
    chooseIfWatched(ev) {
        this.parms.isWatched = ev;
        this.isWatched = ev;
        this.getLiveShowDetail(this.parms)
    }

    private isReview = '';

    /**
     * 是否观看
     */
    chooseIfReview(ev) {
        this.parms.isPlayBack = ev;
        this.isReview = ev;
        this.getLiveShowDetail(this.parms)
    }

    /**
     * 请求直播详情
     */
    getLiveShowDetail(parms) {
        this.isLoaded = false;
        this.load.showLoading();
        this.liveShowDetailPro.getLiveShowDetailData(parms).subscribe(res => {
            this.load.dismissLoading();
            this.isLoaded = true;
            this.liveShowDetailData = JSON.parse(res);
            this.liveShowDetailData.groupList.unshift({'name': '全部', 'id': ''});
        }, error2 => {
            this.load.dismissLoading();
        })
    }

    /**
     * 返回
     * @param ev
     */
    backButtonClick(ev) {
        ev.preventDefault();
        ev.stopPropagation();
        this.navCtrl && this.navCtrl.pop(null, null);
    }


    /**
     * 回到顶部
     */
    goUp() {
        if (this.isScrolling) {
            setTimeout(() => {
                this.goUp();
            }, 300);
            return;
        }
        this.content.scrollToTop();
    }

    /**
     * 滚动开始
     * @param e
     */
    scrollStart(e) {
        this.isScrolling = true;
    }

    /**
     * 滚动结束
     * @param e
     */
    scrollEnd(e) {
        this.isScrolling = false;
    }

}
